package com.sp.system.user.customer.controller;


import com.sp.system.user.core.annotation.LoginAuthentication;
import com.sp.system.user.core.entity.User;
import com.sp.system.user.core.utils.ContentResult;
import com.sp.system.user.customer.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistry;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Value("${spring.application.name}")
    private String severname;

    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    IUserService userService;
    @Autowired
    private ZookeeperServiceRegistry serviceRegistry;

    @LoginAuthentication
    @RequestMapping("/queryUserList")
    public Object queryUserList(){

        List userList = userService.queryUserList();

        List<ServiceInstance> serviceInstances = discoveryClient.getInstances(severname);
        //获取实例化的服务
        StringBuffer sb = new StringBuffer();
        if (serviceInstances != null && serviceInstances.size() > 0 ) {
            sb.append(serviceInstances.get(0).getUri()+",");
        }

        return ContentResult.successResult(userList,sb.toString());
    }

    @RequestMapping("/geterror")
    public Object geterror(){
        
        User user = new User();
        user.setUserName("zhangsan");
        user.setPassword("123456");
        return ContentResult.errorResult(user,"111c");
    }

}
